package com.example.provider.service.Impl;


import com.example.provider.mapper.UserMapper;
import com.guo.constant.Root;
import com.guo.constant.common;
import com.guo.pojo.*;
import com.guo.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = Logger.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper mapper;


    // 查询所有用户信息
    @Override
    public List<User> getOne() {
        logger.info("查询所有用户信息");
        return mapper.getOne();
    }

    //根据用户名和密码查询用户信息
    @Override
    public User getUserByName_Pwd(String name, String password) {
        logger.info("使用用户名和密码获取用户信息");
        return mapper.getUserByName_Pwd(name,password);
    }

    //添加用户信息---注册
    @Override
    public String addUser(User user) {
        logger.info("注册一个用户");
        //首先判断参数是否为空
        if (user == null){
            return common.PARAM_EMPTY;
        }
        // 添加的用户名是否被使用
        User userByName = mapper.getUserByName_Pwd(user.getRoleName(), null);
        if (userByName != null){
            return common.NAME_EXIST;
        }
        //设置权限id，默认是3 游客
        user.setRootId(Root.VISITOR);
        mapper.addUser(user);
        return common.ADD_SUCCESS;
    }



    //根据用户信息查询对应的权限
    @Override
    public String getRoot(String name) {
        logger.info("根据用户名查询用户权限");
        com.guo.pojo.Root root = mapper.getRoot(name);
        if (root == null){
            //没有对应的记录
            return common.PARAM_EMPTY;
        }
        return root.getRootName();
    }


    //分页查询-模糊查询
    @Override
    public List<Staff> getByPage(String name, Page page) {
        logger.info("用户名模糊查询和分页查询");
        return mapper.getByPage(name,page);
    }

    @Override
    public Staff_Salary_Dept getStaffInfo(String name) {
        logger.info("通过员工名称查询员工部门和工资");
        // 检查参数
        if (name == null){
            return null;
        }
        //查询当前员工的部门
        Department dept = mapper.getDept(name);
        //查询当前员工的工资
        Salary salary = mapper.getSalary(name);
        //没查询到结果
        if (dept == null || salary == null){
            return null;
        }
        //赋值
        Staff_Salary_Dept salary_dept = new Staff_Salary_Dept();
        salary_dept.setName(name);
        salary_dept.setDepartment(dept.getDepartment());
        salary_dept.setSalary(salary.getBasicSalary()+salary.getBonus());
        return salary_dept;
    }

    @Override
    public String addStaff(Staff staff) {
        logger.info("添加一个员工");
        if (staff == null){
            return common.PARAM_EMPTY;
        }
        mapper.addStaff(staff);
        return common.ADD_SUCCESS;
    }


    @Override
    public String updateStaff(Staff staff) {
        logger.info("根据员工id修改员工信息");
        if (staff == null){
            return common.PARAM_EMPTY;
        }
        mapper.updateStaff(staff);
        return common.UPDATE_SUCCESS;
    }

    @Override
    public String deleteStaff(Integer id) {
        logger.info("根据员工id删除员工信息");
        if (id == null){
            return common.PARAM_EMPTY;
        }
        mapper.deleteStaff(id);
        return common.DELETE_SUCCESS;
    }
}
